<template>
    <div class="orderListRuleForm">
        <panle title='推广员列表' content="推广员列表"></panle>
        <!--搜索-->
        <el-form :inline="true" ref="form" :model="form" label-width="100px" class="demo-form-inline">
            <el-form-item label="邀请时间" prop="apply_for_time">
                <el-date-picker
                    v-model="form.invite_time"
                    type="daterange"
                    align="right"
                    size="mini"
                    unlink-panels
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    format="yyyy-MM-dd"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    :default-time="['00:00:00', '23:59:59']">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="姓名:" prop="popu_name">
                <el-input size="mini" placeholder="请输入" v-model="form.popu_name"></el-input>
            </el-form-item>
            <el-form-item label="推荐人:" prop="invite_user_name">
                <el-input size="mini" placeholder="请输入" v-model="form.invite_user_name"></el-input>
            </el-form-item>
            <el-form-item label="状态:" prop="status">
                <el-select v-model="form.status" placeholder="请选择" size="mini">
                    <el-option label="禁用" value="0"></el-option>
                    <el-option label="正常" value="1"></el-option>
                    <el-option label="离职" value="2"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="级别:" prop="popu_level">
                <el-select v-model="form.popu_level" placeholder="请选择" size="mini">
                    <el-option label="一级推广员" value="1"></el-option>
                    <el-option label="二级推广员" value="2"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="search" size="mini">搜索</el-button>
                <el-button @click="resetForm('form')" size="mini">重置</el-button>
                <el-button size="mini" @click="init()">刷新</el-button>
            </el-form-item>
        </el-form>
        <!--列表-->
        <div class="table-wrap">
            <el-table :data="tableData" v-loading="loading" style="width: 100%;margin-bottom: 20px;" row-key="id" border stripe default-expand-all>
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column prop="popu_name" label="姓名"></el-table-column>
                <el-table-column prop="phone" label="手机号"></el-table-column>
                <el-table-column prop="popu_level" label="级别">
                    <template slot-scope="scope">
                        <span v-if="scope.row.popu_level == 1">一级</span>
                        <span v-else>二级</span>
                    </template>
                </el-table-column>
                <el-table-column prop="invite_user_name" label="推荐人"></el-table-column>
                <el-table-column prop="status" label="状态">
                    <template slot-scope="scope">
                        <span v-if="scope.row.status == 0">禁用</span>
                        <span v-else-if="scope.row.status == 1">正常</span>
                        <span v-else>离职</span>
                    </template>
                </el-table-column>
                <el-table-column prop="invite_time" label="邀请时间"></el-table-column>
                <el-table-column prop="inherit_name" label="继承人"></el-table-column>
                <el-table-column prop="inherit_time" label="继承时间"></el-table-column>
                <el-table-column prop="id" label="操作">
                    <template slot-scope="scope">
                        <PersButton type="text" v-if="scope.row.status == 1" size="mini" label="离职" icon="fa  fa-pencil" @click="quitUser(scope.row)"></PersButton>
                        <PersButton type="text" v-if="scope.row.status == 2 && scope.row.inherit_id == 0" size="mini" label="继承" icon="fa  fa-pencil" @click="inheritUser(scope.row)"></PersButton>
                        <span v-if="scope.row.status == 2 && scope.row.inherit_id != 0">已被继承</span>
                    </template>
                </el-table-column>
            </el-table>
            <div class="block">
                <el-pagination @size-change="handlePopuuserSizeChange" @current-change="handlePopuuserCurrentChange"
                               :current-page="form.pagePopuuserNumber" :page-sizes="[10, 50, 100, 200]"
                               :page-size="form.pagePopuuserSize"
                               background layout="total, sizes, prev, pager, next, jumper"
                               :total="form.popuuserTotal">
                </el-pagination>
            </div>
        </div>
        <!--离职-->
        <el-dialog :visible.sync="quitOpen" :before-close="handleClose" width="1000px" append-to-body title="离职确认">
            <span>员工姓名：{{quitForm.popu_name}}</span>
            <el-form ref="quitForm" :model="quitForm">
                <el-row>
                    <el-col :span="14">
                        <el-form-item label="备注：" prop="remark">
                            <el-input size="mini" class="w260" v-model="quitForm.remark"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="handleClose" size="small">取 消</el-button>
                <el-button type="primary" @click="submitquitForm('quitForm') " size="small">确 定</el-button>
            </div>
        </el-dialog>
        <!--继承人弹框-->
        <el-dialog :visible.sync="inviteOpen" :before-close="handleClose" width="1000px" append-to-body title="选择继承推广员">
            <el-form :inline="true" ref="staffForm" :model="staffForm" label-width="100px" class="demo-form-inline">
                <el-form-item label="推广员昵称:" prop="popu_name">
                    <el-input size="mini" placeholder="请输入" v-model="staffForm.popu_name"></el-input>
                </el-form-item>
                <el-form-item label="唯一标识:" prop="popu_yard">
                    <el-input size="mini" placeholder="请输入" v-model="staffForm.popu_yard"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="searchInvite" size="mini">搜索</el-button>
                </el-form-item>
            </el-form>
            <div class="table-wrap">
                <el-table :data="inviteStaffData" v-loading="loading" style="width: 100%;margin-bottom: 20px;" row-key="id" border stripe default-expand-all>
                    <el-table-column prop="popu_name" label="推广员"></el-table-column>
                    <el-table-column prop="popu_yard" label="唯一标识"></el-table-column>
                    <el-table-column prop="junior_num" label="下级数量"></el-table-column>
                    <el-table-column prop="is_invite" label="是否继承过"></el-table-column>
                    <el-table-column prop="id" label="操作">
                        <template slot-scope="scope">
                            <PersButton type="text" size="mini" label="继承" icon="fa  fa-pencil" @click="inherit(scope.row)"></PersButton>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="block">
                    <el-pagination @size-change="handleInviteSizeChange" @current-change="handleInviteCurrentChange"
                                   :current-page="staffForm.pageInviteNumber" :page-sizes="[10, 50, 100, 200]"
                                   :page-size="staffForm.pageInviteSize"
                                   background layout="total, sizes, prev, pager, next, jumper"
                                   :total="staffForm.inviteTotal">
                    </el-pagination>
                </div>
            </div>
        </el-dialog>
        <!--执行继承操作-->
        <el-dialog :visible.sync="toInviteOpen" :before-close="handleClose" width="1000px" append-to-body title="提示">
            <span>
                确定由该推广员继承{{ needInviteData.popu_name }}全部下级并享受其接下来全部下级分佣？操作后将不可更改
            </span>
            <div slot="footer" class="dialog-footer">
                <el-button @click="handleClose" size="small">取 消</el-button>
                <el-button type="primary" @click="subToInviteForm('replenishForm') " size="small">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    getList,quit,getCanInheritStaffList,inherit
} from "@/api/sell/popuuser/popuuser";

import Panle from '@/components/Panle';
import PersButton from '@/components/PersButton';
import {hasPermission, isShow} from '@/common/permission';
import {mapState} from "vuex";

export default {
    name:'Popuuser',
    components: {
        Panle,
        PersButton,
    },
    computed:{
        ...mapState({
            userInfo: state => state.member.userInfo,
        }),
    },
    data() {
        return {
            loading:false,
            examineOpen: false, //审核弹框是否显示
            inviteOpen:false, //继承人是否显示
            quitOpen:false, //离职操作是否显示
            toInviteOpen:false,
            form: {
                invite_time:[],
                popu_name:'',
                invite_user_name:'',
                status:'',
                popu_level:'',
                pagePopuuserSize: 10,
                pagePopuuserNumber: 1,
                popuuserTotal: 0,

            },
            staffForm:{
                popu_name:'',
                popu_yard:'',
                curretnId:'', //需要剔除的员工ID
                pageInviteSize: 10,
                pageInviteNumber: 1,
                inviteTotal: 0,
            },
            quitForm:{
                popu_name:'',
                id:'',
                remark:''
            },
            tableData:[],
            inviteStaffData:[],
            toInvitData:[], //继承链接的员工
            needInviteData:[], //继承给员工的数据
        }
    },
    created() {
        this.init(true);
    },
    methods: {
        hasPermission,
        isShow,
        init() {
            this.loading = true;
            this.form.page = this.form.pagePopuuserNumber;
            this.form.size = this.form.pagePopuuserSize;
            getList(this.form).then((res) => {
                this.tableData = res.data.data;
                this.form.popuuserTotal = res.data.total;
                this.loading = false;
            })
        },
        //获取可以继承的员工列表
        getCanInheritStaffList(){
            this.loading = true;
            getCanInheritStaffList(this.staffForm).then((res) => {
                this.inviteStaffData = res.data.data;
                this.staffForm.inviteTotal = res.data.total;
                this.loading = false;
                this.inviteOpen = true;
            })
        },

        //操作后初始化
        initPopu(){

        },

        //关闭弹框回调
        handleClose(){
            this.quitOpen = false;
            this.inviteOpen = false;
            this.toInviteOpen = false;
        },

        search: function () {
            this.loading = true;
            this.form.pagePopuuserSize = 10;
            this.form.pagePopuuserNumber = 1;
            this.init();
        },
        searchInvite: function () {
            this.loading = true;
            this.staffForm.pageInviteSize = 10;
            this.staffForm.pageInviteNumber = 1;
            this.getCanInheritStaffList();
        },
        handlePopuuserSizeChange: function (e) {
            this.form.pageExamineSize = e;
            this.init();
        },
        handlePopuuserCurrentChange: function (e) {
            this.form.pageExamineNumber = e;
            this.init();
        },
        handleInviteSizeChange: function (e) {
            this.staffForm.pageInviteSize = e;
            this.getCanInheritStaffList();
        },
        handleInviteCurrentChange: function (e) {
            this.staffForm.pageInviteNumber = e;
            this.getCanInheritStaffList();
        },
        resetForm(formName) {
            this.form.invite_time = [];
            this.form.popu_name = '';
            this.form.invite_user_name = '';
            this.form.status = '';
            this.form.popu_level = '';
            this.init();
        },
        //离职
        quitUser:function (data){
            this.quitOpen = true;
            this.quitForm.popu_name = data.popu_name;
            this.quitForm.id = data.id;
        },
        //离职提交
        submitquitForm:function(data){
            this.$refs[data].validate((valid) => {
                if (valid) {
                    var param = {
                        'id':this.quitForm.id,
                        'remark':this.quitForm.remark
                    }
                    quit(param).then((res)=>{
                        if(res.code==200){
                            this.msgSuccess(res.msg);
                            this.init();
                            this.quitOpen=false;
                        }else{
                            this.msgError(res.msg);
                        }
                    })
                }else{
                    return false;
                }
            })
        },
        //继承
        inheritUser:function(data){
            this.loading = true;
            this.staffForm.curretnId = data.id;
            this.needInviteData = data;
            getCanInheritStaffList(this.staffForm).then((res) => {
                this.inviteStaffData = res.data.data;
                this.staffForm.inviteTotal = res.data.total;
                this.loading = false;
                this.inviteOpen = true;
            })
        },
        //继承操作弹框
        inherit:function (e){
            this.toInviteOpen = true;
            this.toInvitData = e;
        },
        //继承操作
        subToInviteForm:function (){
            var param = {
                'inherit_id':this.toInvitData.id,
                'id':this.needInviteData.id
            }
            inherit(param).then((res)=>{
                if(res.code==200){
                    this.msgSuccess(res.msg);
                    this.toInviteOpen=false;
                    this.inviteOpen=false;
                    this.init();
                }else{
                    this.msgError(res.msg);
                }
            })
        }
    }
}

</script>

<style lang="scss" scoped>
.demo-form-inline {
    margin: 20px;

    .el-form-item {
        margin-bottom: 0;
    }
}
.el-form--inline .el-form-item {
    display: inline-block;
    vertical-align: top;
}

.orderListRuleForm .el-form-item {
    margin-bottom: 0;
}

.search-content {
    width: 100%;
    margin: 20px;
}

.create_link{
    margin-top: 7px;
    float: right;
}

.w50{
    width: 50px !important;
}
.w60{
    width: 60px !important;
}
.w260{
    width: 260px !important;
}
.mr20{
    margin-right: 20px;
}
</style>
